package com.das.mechanic_base.adapter.camera;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.bean.alone.AlonePicVideoBean;
import com.das.mechanic_base.utils.X3GlideNewUtils;
import com.das.mechanic_base.utils.X3ScreenUtils;
import com.otaliastudios.cameraview.controls.Mode;

import java.util.ArrayList;
import java.util.List;

public class X3CameraPreviewAdapter2 extends RecyclerView.Adapter<X3CameraPreviewAdapter2.PreviewHolder> {

    private List<AlonePicVideoBean> mList;

    private Context mContext;

    private List<Boolean> sList;

    private boolean isDelete = true;

    /**
     * 传入类型 是视频还是图片
     */
//    private Mode mode;

    public X3CameraPreviewAdapter2(Context mContext) {
        this.mContext = mContext;
//        this.mode = Mode.PICTURE;
        this.mList = new ArrayList<>();
        this.sList = new ArrayList<>();
    }

    public void changeCameraList(List<AlonePicVideoBean> mList) {
        this.mList = mList;
//        this.mode = mode;
        this.sList.clear();
        for (int i = 0; i < mList.size(); i++) {
            sList.add(false);
        }
        notifyDataSetChanged();
    }

    public void changeIsDelete(boolean isDelete) {
        this.isDelete = isDelete;
        notifyDataSetChanged();
    }

    public void resetCameraPreview() {
        this.sList.clear();
        for (int i = 0; i < mList.size(); i++) {
            sList.add(false);
        }
        notifyDataSetChanged();
    }

    /**
     * 数据源删除之后 再刷新列表 只针对于视频
     * @param position
     */
    public void delMovie(int position) {
        sList.remove(position);
        notifyItemRemoved(position);
        notifyItemRangeChanged(position,mList.size() - position);
    }

    @NonNull
    @Override
    public PreviewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new PreviewHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_camera_preview_item,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull PreviewHolder holder, int position) {

        RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) holder.itemView.getLayoutParams();

        if (position == 0) {
            layoutParams.leftMargin = X3ScreenUtils.dipToPx(16,mContext);
            layoutParams.rightMargin = 0;
        }else if (position == mList.size() - 1) {
            layoutParams.rightMargin = X3ScreenUtils.dipToPx(16,mContext);
            layoutParams.leftMargin = 0;
        }else {
            layoutParams.leftMargin = 0;
            layoutParams.rightMargin = 0;
        }

        holder.itemView.setLayoutParams(layoutParams);

        holder.iv_play.setVisibility((mList.get(position).path.endsWith(".mp4")) ? View.VISIBLE : View.GONE);

        if (isDelete) {
//            holder.iv_delete.setVisibility(View.VISIBLE);
//            holder.iv_border.setVisibility((mode == Mode.PICTURE && sList.get(position)) ? View.VISIBLE : View.INVISIBLE);
//
//            holder.iv_delete.setOnClickListener(view -> {
//                if (iOnCameraPreview != null && position < mList.size()) {
//                    iOnCameraPreview.iOnCameraDelete(mList.get(position).path);
//                }
//                if (mode == Mode.VIDEO) {
//                    return;
//                }
//                if (position >= 0 && position < sList.size()) {
//                    sList.remove(position);
//                    notifyItemRemoved(position);
//                    notifyItemRangeChanged(position,mList.size() - position);
//                }
//            });

        }else {
            holder.iv_border.setVisibility(View.INVISIBLE);
            holder.iv_delete.setVisibility(View.INVISIBLE);
        }

        X3GlideNewUtils.loadRoundCornerImage(mContext,mList.get(position).path,holder.iv_image, X3ScreenUtils.dipToPx(8,mContext), Color.BLACK);

        holder.itemView.setOnClickListener(view -> {
            if (mList.get(position).mode == Mode.VIDEO) {
                return;
            }
            for (int i = 0; i < sList.size(); i++) {
                if (i == position) {
                    if (sList.get(i)) {
                        sList.remove(i);
                        sList.add(i,false);
                    }else {
                        sList.remove(i);
                        sList.add(i,true);
                    }
                }else {
                    sList.remove(i);
                    sList.add(i,false);
                }
            }

            if (iOnCameraPreview != null && position < mList.size() && position < sList.size()) {
                iOnCameraPreview.iOnCameraPreview(mList.get(position).path,sList.get(position));
            }

            notifyDataSetChanged();
        });
    }

    IOnCameraPreview iOnCameraPreview;

    public void setiOnCameraPreview(IOnCameraPreview iOnCameraPreview) {
        this.iOnCameraPreview = iOnCameraPreview;
    }

    public interface IOnCameraPreview {

        /**
         * 预览
         * @param path
         */
        void iOnCameraPreview(String path,boolean isPreview);

        /**
         * 删除该图
         * @param path
         */
        void iOnCameraDelete(String path);
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class PreviewHolder extends RecyclerView.ViewHolder {
        ImageView iv_border;
        ImageView iv_image;
        ImageView iv_delete;
        ImageView iv_play;
        public PreviewHolder(@NonNull View itemView) {
            super(itemView);
            iv_border = itemView.findViewById(R.id.iv_border);
            iv_image = itemView.findViewById(R.id.iv_image);
            iv_play = itemView.findViewById(R.id.iv_play);
            iv_delete = itemView.findViewById(R.id.iv_delete);
        }
    }
}
